#include "widget.h"
#include "ui_widget.h"
#include <QNetworkReply>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setWindowTitle("客户端");
    manager = new QNetworkAccessManager(this);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    // 获取输入框url
    QUrl url(ui->lineEdit->text());
    // 构造http请求
    QNetworkRequest request(url);
    // 发送请求
    QNetworkReply* reply = manager->get(request);
    // 信号槽处理响应
    connect(reply, &QNetworkReply::finished, this, [=]
    {
        if (reply->error() == QNetworkReply::NoError)
        {
            // 响应正确收到
            QString text = reply->readAll();
            ui->plainTextEdit->setPlainText(text);
        }
        else
        {
            // 响应出错
            ui->plainTextEdit->setPlainText(reply->errorString());
        }
        // 释放reply
        reply->deleteLater();
    });
}
